﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Paxnam.GameStates
{
    class Level_01
    {
        #region Atributos

        Texture2D board;

        Rectangle boardRect;

        Color[] boardColors;

        GameWindow Window;
        
        Player player_1;
        Player player_2;

        PillManager pillManager;

        Song bg_Music;

        //detecção de colisao de cor sem classes:
        //retangulos

        Rectangle j1, j2;

        //cores
        Color[] c1, c2;

        #endregion
        public Level_01(ContentManager Content, GameWindow Window)
        {
            this.Window = Window;
            pillManager = new PillManager(Content,Window);

            player_1 = new Player(Window,Content.Load<Texture2D>("Pacman"));
            player_1.playerId = 1;
            player_1.Position = new Vector2(390, 458);

            player_2 = new Player(Window, Content.Load<Texture2D>("Pacman"));
            player_2.Position = new Vector2(150, 150);
          

            j1 = player_1.CollisionRect;
            j2 = player_2.CollisionRect;

            c1 = new Color[player_1.SizeTexture()];
            c2 = new Color[player_2.SizeTexture()];

            player_1.Texture.GetData(c1);
            player_2.Texture.GetData(c2);

            
            board = Content.Load<Texture2D>("level1");
            boardRect = new Rectangle(0, 0, board.Width, board.Height);
            boardColors = new Color[board.Width * board.Height];
            board.GetData(boardColors);
            
            
        }
        public void Update(GameTime gameTime)
        {
             player_1.Update(gameTime, Game1.keyboardState);
             player_2.Update(gameTime, Game1.keyboardState);

           
             //Console.WriteLine("" + board.Width + "\n" + board.Height);

            //pillManager.Update(gameTime, player_1);

             //COLISAO DE COR

             j1 = player_1.CollisionRect;
             j2 = player_2.CollisionRect;

             c1 = new Color[player_1.SizeTexture()];
             c2 = new Color[player_2.SizeTexture()];

             player_1.Texture.GetData(c1);
             player_2.Texture.GetData(c2);

             if (ColisaoPorPixel(j1, boardRect, c1, boardColors) && !player_1.colidiu)
             {
                 player_1.previous_direction = player_1.actual_direction;
                 player_1.actual_direction = GameObject.Direction.STOPPED;
                 player_1.Velocity = Vector2.Zero;
                 player_1.colidiu = true;
                 //Console.WriteLine("AAAAAAHHH!!!!!!!!!!!");
                 //Console.WriteLine("TESTE!!!!!");
                 //Console.WriteLine("BATI!!!");
                 

                 switch (player_1.previous_direction)
                 {
                     case GameObject.Direction.RIGHT:
                         player_1.Position = new Vector2(player_1.Position.X - 14, player_1.Position.Y);
                         break;
                     case GameObject.Direction.LEFT:
                         player_1.Position = new Vector2(player_1.Position.X + 14, player_1.Position.Y);
                         break;
                     case GameObject.Direction.UP:
                         player_1.Position = new Vector2(player_1.Position.X, player_1.Position.Y + 24);
                         break;
                     case GameObject.Direction.DOWN:
                         player_1.Position = new Vector2(player_1.Position.X, player_1.Position.Y - 14);
                         break;
                 }
                 
             }
             else
             {
                 player_1.previous_direction = player_1.actual_direction;
                 player_1.Velocity = new Vector2(3, 3);
                 player_1.colidiu = false;
             //    player_1.actual_direction = player_1.previous_direction;
             //    Console.WriteLine("NÃO BATI!!!");
             }
          
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(board, Vector2.Zero, Color.White);

            player_1.Draw(gameTime, spriteBatch);

            player_2.Draw(gameTime, spriteBatch);

            //pillManager.Draw(gameTime, spriteBatch);
           
        }

        // Determina se há sobreposição dos pixels não-transparentes entre os dois retangulos
        public bool ColisaoPorPixel(Rectangle j1, Rectangle j2, Color[] c1, Color[] c2)
        {
            // Encontra os limites do retangulo de interseção
            int cima = Math.Max(j1.Top, j2.Top);

            int baixo = Math.Min(j1.Bottom, j2.Bottom);

            int esquerda = Math.Max(j1.Left, j2.Left);

            int direita = Math.Min(j1.Right, j2.Right);

            // Verifica todos os pontos dentro do limite de intereseção 
            //TODO verificar se os retangulos estao um ao lado do outro
            for (int y = cima; y < baixo; y++)
            {
                for (int x = esquerda; x < direita; x++)
                {
                    // Verifica a cor de ambos os pixels neste momento
                    Color color1 = c1[(x - j1.Left) +
                    (y - j1.Top) * j1.Width];
                    Color color2 = c2[(x - j2.Left) +
                    (y - j2.Top) * j2.Width];

                    // Se ambos os píxels não são completamente diferentes
                    //if (color1.A != 0 && color2.A != 0)
                    //{
                    //    // Um cruzamento de pixel foi encontrado
                    //    return true;
                    //}
                    if (color1.R == 255 && color1.G == 255 && color1.B == 0 &&
                             color2.R == 88 && color2.G == 74 && color2.B == 138)
                    {
                        
                        return true;
                    }
                }
            }
            // Não foi encontrado cruzamento entre os pixels
            return false;
        }
    }
}
